/*
 * Copyright (c) 2016-2023 The gRPC-Spring Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xydpeng.demo.grpc.service;

import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.config.GrpcServerProperties;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.xydpeng.demo.grpc.lib.HelloReply;
import org.xydpeng.demo.grpc.lib.HelloRequest;
import org.xydpeng.demo.grpc.lib.SimpleGrpc;

/**
 * Example grpc server service implementation class.
 */
@GrpcService
public class GrpcServerService extends SimpleGrpc.SimpleImplBase implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private int port;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        this.port = applicationContext.getBean(GrpcServerProperties.class).getPort();
    }

    @Override
    public void sayHello(final HelloRequest req, final StreamObserver<HelloReply> responseObserver) {
        final HelloReply reply = HelloReply.newBuilder().setMessage(port+" Hello==> " + req.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

}
